package org.company.earth.apache.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class HttpClientUtil {
    
    private static Logger logger = Logger.getLogger(HttpClientUtil.class);
    
    private static HttpClientUtil instance = null;
    
    private HttpClientUtil() {
        
    }
    
    public static HttpClientUtil getInstance() {
        if(instance == null) {
            instance = new HttpClientUtil();
        }
        return instance;
    }
    
    /**
     * 发送Get请求
     * @method sendGet
     * @param uri
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String sendGet(String uri) throws ClientProtocolException, IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet post = new HttpGet(uri);
        CloseableHttpResponse response = httpClient.execute(post);
        String reponseBody = EntityUtils.toString(response.getEntity());
        response.close();
        httpClient.close();
        return reponseBody;
    }
    
    public String sendPost(String uri,Map<String,String> params) throws ClientProtocolException, IOException {
        logger.debug("request xml==【" + uri +"】");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(uri);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        for(Entry<String,String> entry : params.entrySet()) {
            nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        post.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response = httpClient.execute(post);
        String reponseBody = EntityUtils.toString(response.getEntity());
        response.close();
        httpClient.close();
        return reponseBody;
    }
    
    public String sendPost(String uri,String content) throws ClientProtocolException, IOException {
        logger.debug("request xml==【" + content +"】");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(uri);
        post.setEntity(new InputStreamEntity(new ByteArrayInputStream(content.getBytes("UTF-8"))));
        CloseableHttpResponse response = httpClient.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();
        logger.debug("response code==" + statusCode);
        if(statusCode != 200) {
            return null;
        }
        String reponseBody = EntityUtils.toString(response.getEntity());
        response.close();
        httpClient.close();
        return reponseBody;
    }
}
